package com.l2jserver.gameserver.communitybbs.Manager;

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import javolution.text.TextBuilder;

import com.l2jserver.Config;
import com.l2jserver.gameserver.cache.HtmCache;
import com.l2jserver.gameserver.communitybbs.Model.BaseBBSManager;
import com.l2jserver.gameserver.datatables.CharTable;
import com.l2jserver.gameserver.model.BlockList;
import com.l2jserver.gameserver.model.L2World;
import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
import com.l2jserver.gameserver.network.SystemMessageId;
import com.l2jserver.gameserver.network.clientpackets.Say2;
import com.l2jserver.gameserver.network.serverpackets.CreatureSay;
import com.l2jserver.gameserver.network.serverpackets.ShowBoard;
import com.l2jserver.gameserver.network.serverpackets.SystemMessage;

/**
 * @author fissban
 */
public class FriendsBBSManager extends BaseBBSManager
{
	private List<Integer> friendOnline = null;
	private List<Integer> friendOffline = null;
	private List<Integer> friendBlock = null;
	
	@Override
	public void parseCmd(String command, L2PcInstance activeChar)
	{
		if (command.startsWith("_friend"))
		{
			String content = HtmCache.getInstance().getHtm(activeChar.getHtmlPrefix(), "data/html/CommunityBoard/custom/friend/index.htm");
			if (content == null)
			{
				content = "<html><body><br><br><center>404 :File not found: 'data/html/CommunityBoard/custom/friend/index.htm' </center></body></html>";
				separateAndSend(content, activeChar);
				return;
			}
			
			generateList(activeChar);
			
			StringTokenizer st = new StringTokenizer(command, ";");
			st.nextToken();// _friendlist_0_
			
			if (!st.hasMoreTokens())
			{
				content = content.replaceAll("%all%", Head("onlineList") + BodyOnline(1));
			}
			else
			{
				String cmd = st.nextToken();
				String opc = st.nextToken();
				
				switch (cmd)
				{
					case "onlineList":
						content = content.replaceAll("%all%", Head("onlineList") + BodyOnline(Integer.valueOf(opc)));
						break;
					case "offlineList":
						content = content.replaceAll("%all%", Head("offlineList") + BodyOffline(Integer.valueOf(opc)));
						break;
					case "blockList":
						content = content.replaceAll("%all%", Head("blockList") + BodyBlock(Integer.valueOf(opc)));
						break;
					case "private":
						content = content.replaceAll("%all%", Head("private") + BodyPrivate(opc));
						break;
					case "mail":
						// TODO desarollar
						break;
				}
			}
			
			separateAndSend(content, activeChar);
		}
	}
	
	/**
	 * Definimos el encabezado del html.
	 * @param opc q saldra como barra superior.
	 * @return
	 */
	private String Head(String opc)
	{
		TextBuilder tb = new TextBuilder();
		tb.append("<br>");
		
		tb.append("<table border=0 cellspacing=0 cellpadding=0 WIDTH=755 HEIGHT=38 bgcolor=111111>");
		tb.append("<tr>");
		switch (opc)
		{
			case "onlineList":
				tb.append("<td FIXWIDTH=40 HEIGHT=40 align=center valign=top><img src=L2UI_CH3.msnminimumicon1 WIDTH=32 HEIGHT=32></td>");
				tb.append("<td FIXWIDTH=700 HEIGHT=40 align=left valign=center><font name=hs12 color=00CC00>" + friendOnline.size() + " Friends Online</font></td>");
				break;
			case "offlineList":
				tb.append("<td FIXWIDTH=40 HEIGHT=40 align=center valign=top><img src=L2UI_CH3.msnminimumicon4 WIDTH=32 HEIGHT=32></td>");
				tb.append("<td FIXWIDTH=700 HEIGHT=40 align=left valign=center><font name=hs12 color=CC0000>" + friendOffline.size() + " Friends Offline</font></td>");
				break;
			case "blockList":
				tb.append("<td FIXWIDTH=40 HEIGHT=40 align=center valign=top><img src=L2UI_CH3.msnminimumicon2 WIDTH=32 HEIGHT=32></td>");
				tb.append("<td FIXWIDTH=700 HEIGHT=40 align=left valign=center><font name=hs12 color=FFFF00>" + friendBlock.size() + " Users block</font></td>");
				break;
			case "private":
				tb.append("<td FIXWIDTH=40 HEIGHT=40 align=center valign=top><img src=L2UI_CH3.mailbtn_over WIDTH=32 HEIGHT=32></td>");
				tb.append("<td FIXWIDTH=700 HEIGHT=40 align=left valign=center><font name=hs12 color=B03333>Send Private message</font></td>");
				break;
			case "mail":
				tb.append("<td FIXWIDTH=40 HEIGHT=40 align=center valign=top><img src=L2UI_CH3.mailbtn_over WIDTH=32 HEIGHT=32></td>");
				tb.append("<td FIXWIDTH=700 HEIGHT=40 align=left valign=center><font name=hs12 color=B03333>Send Mail</font></td>");
				break;
		}
		tb.append("</tr>");
		tb.append("</table>");
		
		tb.append("<table border=0 cellspacing=0 cellpadding=1 width=400 HEIGHT=32>");
		tb.append("<tr>");
		tb.append("<td FIXWIDTH=256 HEIGHT=32 align=center><button value=\"View Online\" action=\"bypass _friend;onlineList;1\" back=L2UI_CT1.Tab_DF_Tab_Selected fore=L2UI_CT1.Tab_DF_Tab_Unselected WIDTH=200 HEIGHT=32/></td>");
		tb.append("<td FIXWIDTH=256 HEIGHT=32 align=center><button value=\"View Offline\" action=\"bypass _friend;offlineList;1\" back=L2UI_CT1.Tab_DF_Tab_Selected fore=L2UI_CT1.Tab_DF_Tab_Unselected WIDTH=200 HEIGHT=32/></td>");
		tb.append("<td FIXWIDTH=256 HEIGHT=32 align=center><button value=\"View Block\" action=\"bypass _friend;blockList;1\" back=L2UI_CT1.Tab_DF_Tab_Selected fore=L2UI_CT1.Tab_DF_Tab_Unselected WIDTH=200 HEIGHT=32/></td>");
		tb.append("</tr>");
		tb.append("</table>");
		
		return tb.toString();
	}
	
	/**
	 * Lista de usuarios bloqueados
	 * @param page
	 * @return
	 */
	private String BodyBlock(int page)
	{
		final int limit = 20;
		final int contadorFinal = limit * page; // cantidad de usuarios a mostrar
		final int contadorInicial = contadorFinal - (limit); // donde arrancara a mostrar
		
		TextBuilder tb = new TextBuilder();
		
		// barra con leyenda
		tb.append("<table border=0 cellspacing=0 cellpadding=0 WIDTH=755 HEIGHT=16>");
		tb.append("<tr>");
		tb.append("<td FIXWIDTH=116 HEIGHT=18 align=center valign=center><button action=\"\" value=\"Name\" WIDTH=116 HEIGHT=18 back=L2UI_CT1.button_df fore=L2UI_CT1.button_df></td>");
		tb.append("<td FIXWIDTH=100 HEIGHT=18 align=center valign=center><button action=\"\" value=\"Level\" WIDTH=100 HEIGHT=18 back=L2UI_CT1.button_df fore=L2UI_CT1.button_df></td>");
		tb.append("<td FIXWIDTH=100 HEIGHT=18 align=center valign=center><button action=\"\" value=\"Sex\" WIDTH=100 HEIGHT=18 back=L2UI_CT1.button_df fore=L2UI_CT1.button_df></td>");
		tb.append("<td FIXWIDTH=146 HEIGHT=18 align=center valign=center><button action=\"\" value=\"PvP\" WIDTH=146 HEIGHT=18 back=L2UI_CT1.button_df fore=L2UI_CT1.button_df></td>");
		tb.append("<td FIXWIDTH=146 HEIGHT=18 align=center valign=center><button action=\"\" value=\"Pk\" WIDTH=146 HEIGHT=18 back=L2UI_CT1.button_df fore=L2UI_CT1.button_df></td>");
		tb.append("<td FIXWIDTH=146 HEIGHT=18 align=center valign=center><button action=\"\" value=\"Dead\" WIDTH=146 HEIGHT=18 back=L2UI_CT1.button_df fore=L2UI_CT1.button_df></td>");
		tb.append("</tr>");
		tb.append("</table>");
		// barra con leyenda
		
		for (int cont = contadorInicial; (cont < contadorFinal) && (cont != friendBlock.size()); cont++)
		{
			String name = CharTable.getInstance().getNameById(friendBlock.get(cont));
			
			tb.append("<table border=0 cellspacing=0 cellpadding=0 width=755 HEIGHT=16 bgcolor=" + ColorTable(cont) + ">");
			tb.append("<tr>");
			tb.append("<td FIXWIDTH=16 HEIGHT=18 align=center><img src=L2UI_CH3.msnicon5 width=16 HEIGHT=16></td>");
			tb.append("<td FIXWIDTH=100 HEIGHT=18 align=center valign=left>" + name + "</td>");
			tb.append("<td FIXWIDTH=100 HEIGHT=18 align=center valign=center>" + CharTable.getInstance().getCharLevelById(friendBlock.get(cont)) + "</td>");
			
			if (CharTable.getInstance().getCharSexById(friendBlock.get(cont)) == 1)
			{
				tb.append("<td FIXWIDTH=100 HEIGHT=18 align=center valign=center><font color=FF4040>Female</font></td>");
			}
			else
			{
				tb.append("<td FIXWIDTH=100 HEIGHT=18 align=center valign=center><font color=6161FF>Male</font></td>");
			}
			
			// pvp
			tb.append("<td FIXWIDTH=16 HEIGHT=18 align=center valign=center><img src=L2UI_CH3.redleader WIDTH=16 HEIGHT=16></td>");
			tb.append("<td FIXWIDTH=130 HEIGHT=18 align=center valign=center>" + String.valueOf(CharTable.getInstance().getCharPvp(friendBlock.get(cont))) + "</td>");
			// pk
			tb.append("<td FIXWIDTH=16 HEIGHT=18 align=center valign=center><img src=L2UI_CH3.redsword WIDTH=16 HEIGHT=16></td>");
			tb.append("<td FIXWIDTH=130 HEIGHT=18 align=center valign=center>" + String.valueOf(CharTable.getInstance().getCharPk(friendBlock.get(cont))) + "</td>");
			// death
			tb.append("<td FIXWIDTH=16 HEIGHT=18 align=center valign=center><img src=L2UI_CH3.redattleader WIDTH=16 HEIGHT=16></td>");
			tb.append("<td FIXWIDTH=130 HEIGHT=18 align=center valign=center>" + String.valueOf(CharTable.getInstance().getCharDead(friendBlock.get(cont))) + "</td>");
			// tb.append("<td FIXWIDTH=400 HEIGHT=18 align=left><a action=\"bypass _friend;mail;" + name + "\">Send Mail</a></td>");
			tb.append("</tr>");
			tb.append("</table>");
		}
		
		tb.append("<table border=0 cellspacing=1 cellpadding=1 HEIGHT=30>");
		tb.append("<tr>");
		tb.append("<td><button action=\"bypass _friend;blockList;1\" value=" + 1 + " WIDTH=50 HEIGHT=30 back=L2UI_CT1.Tab_DF_Tab_Selected fore=L2UI_CT1.Tab_DF_Tab_Unselected></td>");
		
		int boton = 1;
		int pag = limit;
		for (int cont = 0; cont <= friendBlock.size(); cont++)
		{
			if (cont > pag)
			{
				boton++;
				pag += limit;
				tb.append("<td><button action=\"bypass _friend;blockList;" + boton + "\" value=" + boton + " WIDTH=30 HEIGHT=30 back=L2UI_CT1.Tab_DF_Tab_Selected fore=L2UI_CT1.Tab_DF_Tab_Unselected></td>");
			}
		}
		tb.append("</tr>");
		tb.append("</table>");
		return tb.toString();
	}
	
	/**
	 * Lista de friends offline
	 * @param page
	 * @return
	 */
	private String BodyOffline(int page)
	{
		final int limit = 20;
		final int contadorFinal = limit * page; // cantidad de usuarios a mostrar
		final int contadorInicial = contadorFinal - (limit); // donde arrancara a mostrar
		
		TextBuilder tb = new TextBuilder();
		
		// barra con leyenda
		tb.append("<table border=0 cellspacing=0 cellpadding=0 WIDTH=755 HEIGHT=16>");
		tb.append("<tr>");
		tb.append("<td FIXWIDTH=116 HEIGHT=18 align=center valign=center><button action=\"\" value=\"Name\" WIDTH=116 HEIGHT=18 back=L2UI_CT1.button_df fore=L2UI_CT1.button_df></td>");
		tb.append("<td FIXWIDTH=100 HEIGHT=18 align=center valign=center><button action=\"\" value=\"Level\" WIDTH=100 HEIGHT=18 back=L2UI_CT1.button_df fore=L2UI_CT1.button_df></td>");
		tb.append("<td FIXWIDTH=100 HEIGHT=18 align=center valign=center><button action=\"\" value=\"Sex\" WIDTH=100 HEIGHT=18 back=L2UI_CT1.button_df fore=L2UI_CT1.button_df></td>");
		tb.append("<td FIXWIDTH=146 HEIGHT=18 align=center valign=center><button action=\"\" value=\"PvP\" WIDTH=146 HEIGHT=18 back=L2UI_CT1.button_df fore=L2UI_CT1.button_df></td>");
		tb.append("<td FIXWIDTH=146 HEIGHT=18 align=center valign=center><button action=\"\" value=\"Pk\" WIDTH=146 HEIGHT=18 back=L2UI_CT1.button_df fore=L2UI_CT1.button_df></td>");
		tb.append("<td FIXWIDTH=146 HEIGHT=18 align=center valign=center><button action=\"\" value=\"Dead\" WIDTH=146 HEIGHT=18 back=L2UI_CT1.button_df fore=L2UI_CT1.button_df></td>");
		tb.append("</tr>");
		tb.append("</table>");
		// barra con leyenda
		
		for (int cont = contadorInicial; (cont < contadorFinal) && (cont != friendOffline.size()); cont++)
		{
			String name = CharTable.getInstance().getNameById(friendOffline.get(cont));
			
			tb.append("<table border=0 cellspacing=0 cellpadding=0 WIDTH=755 HEIGHT=16 bgcolor=" + ColorTable(cont) + ">");
			tb.append("<tr>");
			tb.append("<td FIXWIDTH=16 HEIGHT=18 align=center valign=center><img src=L2UI_CH3.msnicon4 WIDTH=16 HEIGHT=16></td>");
			tb.append("<td FIXWIDTH=100 HEIGHT=18 align=center valign=center>" + name + "</td>");
			tb.append("<td FIXWIDTH=100 HEIGHT=18 align=center valign=center>" + CharTable.getInstance().getCharLevelById(friendOffline.get(cont)) + "</td>");
			
			if (CharTable.getInstance().getCharSexById(friendOffline.get(cont)) == 1)
			{
				tb.append("<td FIXWIDTH=100 HEIGHT=18 align=center valign=center><font color=FF4040>Female</font></td>");
			}
			else
			{
				tb.append("<td FIXWIDTH=100 HEIGHT=18 align=center valign=center><font color=6161FF>Male</font></td>");
			}
			
			// pvp
			tb.append("<td FIXWIDTH=16 HEIGHT=18 align=center valign=center><img src=L2UI_CH3.redleader WIDTH=16 HEIGHT=16></td>");
			tb.append("<td FIXWIDTH=130 HEIGHT=18 align=center valign=center>" + String.valueOf(CharTable.getInstance().getCharPvp(friendOffline.get(cont))) + "</td>");
			// pk
			tb.append("<td FIXWIDTH=16 HEIGHT=18 align=center valign=center><img src=L2UI_CH3.redsword WIDTH=16 HEIGHT=16></td>");
			tb.append("<td FIXWIDTH=130 HEIGHT=18 align=center valign=center>" + String.valueOf(CharTable.getInstance().getCharPk(friendOffline.get(cont))) + "</td>");
			// death
			tb.append("<td FIXWIDTH=16 HEIGHT=18 align=center valign=center><img src=L2UI_CH3.redattleader WIDTH=16 HEIGHT=16></td>");
			tb.append("<td FIXWIDTH=130 HEIGHT=18 align=center valign=center>" + String.valueOf(CharTable.getInstance().getCharDead(friendOffline.get(cont))) + "</td>");
			
			// tb.append("<td FIXWIDTH=32 HEIGHT=18 align=center valign=center><img src=L2UI_CH3.msnicon11 WIDTH=16 HEIGHT=16></td>");
			// tb.append("<td FIXWIDTH=400 HEIGHT=18 align=lef valign=centert><a action=\"bypass _friend;mail;" + name + "\">Send Mail</a></td>");
			tb.append("</tr>");
			tb.append("</table>");
			
		}
		tb.append("<table border=0 cellspacing=1 cellpadding=1 HEIGHT=30>");
		tb.append("<tr>");
		tb.append("<td><button action=\"bypass _friend;offlineList;1\" value=" + 1 + " WIDTH=50 HEIGHT=30 back=L2UI_CT1.Tab_DF_Tab_Selected fore=L2UI_CT1.Tab_DF_Tab_Unselected></td>");
		
		int boton = 1;
		int pag = limit;
		for (int cont = 0; cont <= friendOffline.size(); cont++)
		{
			if (cont > pag)
			{
				boton++;
				pag += limit;
				tb.append("<td><button action=\"bypass _friend;offlineList;" + boton + "\" value=" + boton + " WIDTH=30 HEIGHT=30 back=L2UI_CT1.Tab_DF_Tab_Selected fore=L2UI_CT1.Tab_DF_Tab_Unselected></td>");
			}
		}
		tb.append("</tr>");
		tb.append("</table>");
		return tb.toString();
	}
	
	/**
	 * Lista con los usuarios online
	 * @param page
	 * @return
	 */
	private String BodyOnline(int page)
	{
		final int limit = 20;
		final int contadorFinal = limit * page; // cantidad de usuarios a mostrar
		final int contadorInicial = contadorFinal - (limit); // donde arrancara a mostrar
		
		TextBuilder tb = new TextBuilder();
		
		// barra con leyenda
		tb.append("<table border=0 cellspacing=0 cellpadding=0 WIDTH=755 HEIGHT=16>");
		tb.append("<tr>");
		tb.append("<td FIXWIDTH=115 HEIGHT=18 align=center valign=center><button action=\"\" value=\"Name\" WIDTH=115 HEIGHT=18 back=L2UI_CT1.button_df fore=L2UI_CT1.button_df></td>");
		tb.append("<td FIXWIDTH=50 HEIGHT=18 align=center valign=center><button action=\"\" value=\"Level\" WIDTH=50 HEIGHT=18 back=L2UI_CT1.button_df fore=L2UI_CT1.button_df></td>");
		tb.append("<td FIXWIDTH=50 HEIGHT=18 align=center valign=center><button action=\"\" value=\"Sex\" WIDTH=50 HEIGHT=18 back=L2UI_CT1.button_df fore=L2UI_CT1.button_df></td>");
		tb.append("<td FIXWIDTH=90 HEIGHT=18 align=center valign=center><button action=\"\" value=\"Clan\" WIDTH=90 HEIGHT=18 back=L2UI_CT1.button_df fore=L2UI_CT1.button_df></td>");
		tb.append("<td FIXWIDTH=90 HEIGHT=18 align=center valign=center><button action=\"\" value=\"Ally\" WIDTH=90 HEIGHT=18 back=L2UI_CT1.button_df fore=L2UI_CT1.button_df></td>");
		tb.append("<td FIXWIDTH=90 HEIGHT=18 align=center valign=center><button action=\"\" value=\"PvP\" WIDTH=90 HEIGHT=18 back=L2UI_CT1.button_df fore=L2UI_CT1.button_df></td>");
		tb.append("<td FIXWIDTH=90 HEIGHT=18 align=center valign=center><button action=\"\" value=\"Pk\" WIDTH=90 HEIGHT=18 back=L2UI_CT1.button_df fore=L2UI_CT1.button_df></td>");
		tb.append("<td FIXWIDTH=90 HEIGHT=18 align=center valign=center><button action=\"\" value=\"Dead\" WIDTH=90 HEIGHT=18 back=L2UI_CT1.button_df fore=L2UI_CT1.button_df></td>");
		tb.append("<td FIXWIDTH=90 HEIGHT=18 align=center valign=center><button action=\"\" value=\"Private\" WIDTH=90 HEIGHT=18 back=L2UI_CT1.button_df fore=L2UI_CT1.button_df></td>");
		tb.append("</tr>");
		tb.append("</table>");
		// barra con leyenda
		
		for (int cont = contadorInicial; (cont < contadorFinal) && (cont != friendOnline.size()); cont++)
		{
			L2PcInstance friendPlayer = L2World.getInstance().getPlayer(friendOnline.get(cont));
			
			tb.append("<table border=0 cellspacing=0 cellpadding=0 WIDTH=755 HEIGHT=18 bgcolor=" + ColorTable(cont) + ">");
			tb.append("<tr>");
			tb.append("<td FIXWIDTH=16 HEIGHT=18 align=center valign=center><img src=L2UI_CH3.msnicon1 width=16 HEIGHT=16></td>");
			tb.append("<td FIXWIDTH=99 HEIGHT=18 align=center valign=center>" + friendPlayer.getName() + "</td>");
			tb.append("<td FIXWIDTH=50 HEIGHT=18 align=center valign=center>" + friendPlayer.getLevel() + "</td>");
			
			if (friendPlayer.getAppearance().getSex())
			{
				tb.append("<td FIXWIDTH=50 HEIGHT=18 align=center valign=center><font color=FF4040>Female</font></td>");
			}
			else
			{
				tb.append("<td FIXWIDTH=50 HEIGHT=18 align=center valign=center><font color=6161FF>Male</font></td>");
			}
			
			if (friendPlayer.getClan() != null)
			{
				tb.append("<td FIXWIDTH=90 HEIGHT=18 align=center valign=center>" + friendPlayer.getClan().getName() + "</td>");
				if (friendPlayer.getClan().getAllyId() > 0)
				{
					tb.append("<td FIXWIDTH=90 HEIGHT=18 align=center valign=center>" + friendPlayer.getClan().getAllyName() + "</td>");
				}
				else
				{
					tb.append("<td FIXWIDTH=90 HEIGHT=18 align=center valign=center><font color=E95600>No Ally</font></td>");
				}
			}
			else
			{
				tb.append("<td FIXWIDTH=90 HEIGHT=18 align=center valign=center><font color=E95600>No Clan</font></td>");
				tb.append("<td FIXWIDTH=90 HEIGHT=18 align=center valign=center><font color=E95600>No Ally</font></td>");
			}
			// pvp
			tb.append("<td FIXWIDTH=16 HEIGHT=18 align=center valign=center><img src=L2UI_CH3.redleader WIDTH=16 HEIGHT=16></td>");
			tb.append("<td FIXWIDTH=74 HEIGHT=18 align=center valign=center>" + friendPlayer.getPvpKills() + "</td>");
			// pk
			tb.append("<td FIXWIDTH=16 HEIGHT=18 align=center valign=center><img src=L2UI_CH3.redsword WIDTH=16 HEIGHT=16></td>");
			tb.append("<td FIXWIDTH=74 HEIGHT=18 align=center valign=center>" + friendPlayer.getPkKills() + "</td>");
			// death
			tb.append("<td FIXWIDTH=16 HEIGHT=18 align=center valign=center><img src=L2UI_CH3.redattleader WIDTH=16 HEIGHT=16></td>");
			tb.append("<td FIXWIDTH=74 HEIGHT=18 align=center valign=center>" + friendPlayer.getDead() + "</td>");
			// send MP
			tb.append("<td FIXWIDTH=16 HEIGHT=18 align=center valign=center><img src=L2UI_CH3.msnicon11 WIDTH=16 HEIGHT=16></td>");
			tb.append("<td FIXWIDTH=74 HEIGHT=18 align=center valign=center><a action=\"bypass _friend;private;" + friendPlayer.getName() + "\">Send PM</a></td>");
			
			// tb.append("<td FIXWIDTH=16 HEIGHT=18 align=center valign=center><img src=L2UI_CH3.icn_aboutpoint_down WIDTH=16 HEIGHT=16></td>");
			// tb.append("<td FIXWIDTH=80 HEIGHT=18 align=left><a action=\"bypass _friend;info;" + friendPlayer.getName() + "\">View Info User</a></td>");
			tb.append("</tr>");
			tb.append("</table>");
		}
		
		tb.append("<table border=0 cellspacing=1 cellpadding=1 HEIGHT=30>");
		tb.append("<tr>");
		tb.append("<td><button action=\"bypass _friend;offlineList;1\" value=" + 1 + " WIDTH=50 HEIGHT=30 back=L2UI_CT1.Tab_DF_Tab_Selected fore=L2UI_CT1.Tab_DF_Tab_Unselected></td>");
		
		int boton = 1;
		int pag = limit;
		for (int cont = 0; cont <= friendOnline.size(); cont++)
		{
			if (cont > pag)
			{
				boton++;
				pag += limit;
				tb.append("<td><button action=\"bypass _friend;onlineList;" + boton + "\" value=" + boton + " WIDTH=30 HEIGHT=30 back=L2UI_CT1.Tab_DF_Tab_Selected fore=L2UI_CT1.Tab_DF_Tab_Unselected></td>");
			}
		}
		tb.append("</tr>");
		tb.append("</table>");
		
		return tb.toString();
	}
	
	/**
	 * Generamos un html para poder escribir el texto a enviar como MP a un amigo
	 * @param name
	 * @return
	 */
	private String BodyPrivate(String name)
	{
		final L2PcInstance player = L2World.getInstance().getPlayer(name);
		
		if (player == null)
		{
			return NotExist(name);
		}
		TextBuilder tb = new TextBuilder();
		tb.append("<br>");
		tb.append("<br>");
		tb.append("<multiedit var=pm WIDTH=400 HEIGHT=48>");
		tb.append("<button value=Send action=\"Write _friend PM " + player.getName() + " pm pm pm\" WIDTH=200 HEIGHT=32 back=L2UI_CT1.OlympiadWnd_DF_HeroConfirm_Down fore=L2UI_CT1.OlympiadWnd_DF_HeroConfirm>");
		tb.append("</center>");
		return tb.toString();
	}
	
	/**
	 * Generamos el html por si no se encuentra un player<br>
	 * Usado por si un usuario se desconecta y la lista aun no actualiza al ser buscado por otro player
	 * @param name
	 * @return
	 */
	private static String NotExist(String name)
	{
		TextBuilder tb = new TextBuilder();
		tb.append("<br>");
		tb.append("<table border=0 cellspacing=1 cellpadding=1 WIDTH=755 HEIGHT=40 bgcolor=111111>");
		tb.append("<tr>");
		tb.append("<td FIXWIDTH=100 align=right valign=center><img src=L2UI_CH3.msnwaiticon WIDTH=32 HEIGHT=32></td>");
		tb.append("<td FIXWIDTH=480 align=left valign=center><font color=E95600> the user " + name + "does not exist or offline</font></td>");
		tb.append("<td FIXWIDTH=400 align=center valign=center><button value=\"\" action=\"bypass _friend\" back=L2UI_CT1.MiniMap_DF_MinusBtn_Red fore=L2UI_CT1.MiniMap_DF_MinusBtn_Red WIDTH=32 HEIGHT=32/></td>");
		tb.append("</tr>");
		tb.append("</table>");
		return tb.toString();
	}
	
	/**
	 * Generamos la lista de amigos online, offline y block de un usuario
	 * @param activeChar
	 */
	private void generateList(L2PcInstance activeChar)
	{
		friendOnline = new ArrayList<>();
		friendOffline = new ArrayList<>();
		friendBlock = new ArrayList<>();
		
		for (int friend : activeChar.getFriendList())
		{
			final boolean block = activeChar.getBlockList().isInBlockList(friend);
			final L2PcInstance player = L2World.getInstance().getPlayer(friend);
			
			if ((player != null) && !block)
			{
				friendOnline.add(friend);
			}
			else if (!block)
			{
				friendOffline.add(friend);
			}
			else
			{
				friendBlock.add(friend);
			}
		}
	}
	
	/**
	 * Podemos asignar un color a la tabla dependiendo si es par o impar
	 * @param color
	 * @return
	 */
	private static String ColorTable(int color)
	{
		if ((color % 2) == 0)
		{
			return "8B4513";
		}
		return "291405";
	}
	
	@Override
	public void parseWrite(String ar1, String ar2, String ar3, String ar4, String ar5, L2PcInstance activeChar)
	{
		if (activeChar == null)
		{
			return;
		}
		
		if (ar1.equals("PM"))
		{
			try
			{
				L2PcInstance receiver = L2World.getInstance().getPlayer(ar2);
				
				TextBuilder tb = new TextBuilder();
				tb.append("<html><body>");
				tb.append("<br>");
				tb.append("<table border=0 cellspacing=1 cellpadding=2 WIDTH=700 bgcolor=111111 HEIGHT=40>");
				tb.append("<tr>");
				tb.append("<td FIXWIDTH=100 align=right valign=center><img src=L2UI_CH3.mailbtn_over WIDTH=32 HEIGHT=32></td>");
				tb.append("<td FIXWIDTH=480 align=left valign=center><font color=E95600>Send Private Message</font><br1><font color=AAAAAA>" + ar2 + "</font></td>");
				tb.append("<td FIXWIDTH=120 align=center valign=center><button value=\"\" action=\"bypass _bbsloc\" back=L2UI_CT1.MiniMap_DF_MinusBtn_Red fore=L2UI_CT1.MiniMap_DF_MinusBtn_Red WIDTH=32 HEIGHT=32/></td>");
				tb.append("</tr>");
				tb.append("</table>");
				tb.append("<br>");
				
				int opc = 0;
				
				if (receiver == null)
				{
					tb.append("<br><center><font name=hs12>The player does not exist or is not online</font></center>");
					opc++;
				}
				else if (Config.JAIL_DISABLE_CHAT && receiver.isInJail())
				{
					opc++;
					tb.append("<br><center><font name=hs12>The player is in Jail and can not receive messages</font></center>");
				}
				else if (receiver.isChatBanned())
				{
					opc++;
					tb.append("<br><center><font name=hs12>The player has Ban Chat</font></center>");
				}
				else if (activeChar.isInJail() && Config.JAIL_DISABLE_CHAT)
				{
					opc++;
					tb.append("<br><center><font name=hs12>You can not send messages, you are in Jail</font></center>");
				}
				else if (activeChar.isChatBanned())
				{
					opc++;
					tb.append("<br><center><font name=hs12>You can not send messages, you are in Ban Chat</font></center>");
				}
				
				if (opc > 0)
				{
					tb.append("</body></html>");
					separateAndSend(tb.toString(), activeChar);
				}
				else if (receiver != null)
				{
					if (!receiver.isSilenceMode(activeChar.getObjectId()) && !BlockList.isBlocked(receiver, activeChar))
					{
						receiver.sendPacket(new CreatureSay(activeChar.getObjectId(), Say2.TELL, activeChar.getName(), ar3));
						activeChar.sendPacket(new CreatureSay(activeChar.getObjectId(), Say2.TELL, "->" + receiver.getName(), ar3));
						tb.append("<br><center><font name=hs12>Message Send</font></center></body></html>");
						separateAndSend(tb.toString(), activeChar);
					}
					else
					{
						activeChar.sendPacket(SystemMessage.getSystemMessage(SystemMessageId.THE_PERSON_IS_IN_MESSAGE_REFUSAL_MODE));
						parseCmd("_bbsloc" + receiver.getName(), activeChar);// TODO cambiar
					}
				}
			}
			catch (StringIndexOutOfBoundsException e)
			{
				// ignore
			}
		}
		else
		{// FIXME need test
			String content = "<html><body><br><br><center>the command: " + ar1 + " is not implemented yet</center><br><br></body></html>";
			ShowBoard sb = new ShowBoard(content, "101");
			activeChar.sendPacket(sb);
			activeChar.sendPacket(new ShowBoard(null, "102"));
			activeChar.sendPacket(new ShowBoard(null, "103"));
		}
	}
	
	public static FriendsBBSManager getInstance()
	{
		return SingletonHolder._instance;
	}
	
	private static class SingletonHolder
	{
		protected static final FriendsBBSManager _instance = new FriendsBBSManager();
	}
}